package com.ruiboyun.liveapp.dialog;

import android.app.Activity;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;


import com.ruiboyun.liveapp.R;
import com.ruiboyun.liveapp.base.Url;
import com.ruiboyun.liveapp.bean.Share;
import com.ruiboyun.liveapp.util.tool.FileUtils;
import com.ruiboyun.liveapp.util.tool.ToasUtil;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.io.File;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.onekeyshare.OnekeyShareTheme;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * Created by ${吴世芊} on 2017/6/19下午6:01.
 * Updated by  on
 * ArcheryPlus
 * 本类功能:
 * 说明:
 */

public class SharePopupWindow extends PopupWindow {
    @Bind(R.id.share_title)
    TextView shareTitle;
    private Activity context;
    private Share share;
    private File file;
    private String title;
    private IWXAPI api;

    public SharePopupWindow(Activity context, Share share, String title) {
        super(context);
        this.context = context;
        this.share = share;
        this.title = title;
        initPop();
    }

    public void setTitle(String title) {
        shareTitle.setText(title);
    }

    public SharePopupWindow(Activity context, Share share) {
        super(context);
        this.context = context;
        this.share = share;

        initPop();
    }

    private void initPop() {
        View view = LayoutInflater.from(context).inflate(R.layout.share_popwindow, null);

        ButterKnife.bind(this, view);
        //设置SelectPicPopupWindow的View
        this.setContentView(view);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置PopupWindow可触摸
        this.setTouchable(true);
        //设置非PopupWindow区域是否可触摸
//        this.setOutsideTouchable(false);
        //设置SelectPicPopupWindow弹出窗体动画效果
//        this.setAnimationStyle(R.style.window_animation);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);

        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.logo);
        api = WXAPIFactory.createWXAPI(context, Url.APP_ID, false);

        api.registerApp(Url.APP_ID);

        if (bitmap != null) {
            file = FileUtils.saveImage(context, bitmap, "logo2.png");
        }
    }


    @OnClick({R.id.cancle, R.id.rb_wechat, R.id.rb_wechatcircle, R.id.rb_copy})
    public void onClick(View view) {
        if (share == null) {
            return;
        }
        switch (view.getId()) {

            case R.id.rb_wechat:
//                showPerson();
                showShare(context, Wechat.NAME, true);

                break;
            case R.id.rb_wechatcircle:
//                showCircle();
                showShare(context, WechatMoments.NAME, true);
                break;

            case R.id.rb_copy:
                // 从API11开始android推荐使用android.content.ClipboardManager
                // 为了兼容低版本我们这里使用旧版的android.text.ClipboardManager，虽然提示deprecated，但不影响使用。
                ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
                // 将文本内容放到系统剪贴板里。
                cm.setText(share.getTitle() + " " + share.getContent() + " " + share.getUrl());
                ToasUtil.showCenterToast(context, "已复制到剪切板,赶快去粘贴吧!");
                break;
            case R.id.cancle:
                dismiss();
                break;
        }
    }

    private void showCircle() {

        WXWebpageObject wxWebpageObject = new WXWebpageObject();
        wxWebpageObject.webpageUrl = share.getUrl();
        WXMediaMessage mediaMessage = new WXMediaMessage(wxWebpageObject);
        mediaMessage.title = share.getTitle();
        mediaMessage.description = share.getContent();
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.message = mediaMessage;
        req.scene = SendMessageToWX.Req.WXSceneTimeline;
        api.sendReq(req);
    }

    private void showPerson() {
        WXWebpageObject wxWebpageObject = new WXWebpageObject();
        wxWebpageObject.webpageUrl = share.getUrl();
        WXMediaMessage mediaMessage = new WXMediaMessage(wxWebpageObject);
        mediaMessage.title = share.getTitle();
        mediaMessage.description = share.getContent();
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.message = mediaMessage;
        req.scene = SendMessageToWX.Req.WXSceneSession;
        api.sendReq(req);
    }

    /**
     * 演示调用ShareSDK执行分享
     *
     * @param context
     * @param platformToShare 指定直接分享平台名称（一旦设置了平台名称，则九宫格将不会显示）
     * @param showContentEdit 是否显示编辑页
     */
    public void showShare(Context context, String platformToShare, boolean showContentEdit) {

        Platform platform = ShareSDK.getPlatform(platformToShare);
        if (platform == null) {
            ToasUtil.showCenterToast(context, "请安装客户端");
            return;
        }
        OnekeyShare oks = new OnekeyShare();


        oks.setSilent(!showContentEdit);
        if (platformToShare != null) {
            oks.setPlatform(platformToShare);
        }
        //ShareSDK快捷分享提供两个界面第一个是九宫格 CLASSIC  第二个是SKYBLUE
        oks.setTheme(OnekeyShareTheme.CLASSIC);
        // 在自动授权时可以禁用SSO方式
        oks.disableSSOWhenAuthorize();
        //oks.setAddress("12345678901"); //分享短信的号码和邮件的地址
        oks.setTitle(TextUtils.isEmpty(title) ? share.getTitle() : title);
        //无此字段,qq和空间分享失败,qq和空间通过此字段点击跳转页面
        oks.setTitleUrl(share.getUrl());
        oks.setText(share.getContent());
        if (!TextUtils.isEmpty(share.getLogo())){
            oks.setImageUrl(share.getLogo());
        }else {
            if (file != null) {

                oks.setImagePath(file.getAbsolutePath());
            }
        }

        //oks.setImagePath("/sdcard/test-pic.jpg");  //分享sdcard目录下的图片
//        oks.setImageUrl("http://f1.webshare.mob.com/dimgs/1c950a7b02087bf41bc56f07f7d3572c11dfcf36.jpg");
        oks.setUrl(share.getUrl()); //微信不绕过审核分享链接
//        oks.setFilePath(testVideo);  //filePath用于视频分享
        //我对这条分享的评论，仅在人人网和QQ空间使用，否则可以不提供
        oks.setComment("这个直播很有趣,大家快来看吧!");
        //QZone分享完之后返回应用时提示框上显示的名称
        oks.setSite("华夏云视");
        oks.setSiteUrl(share.getUrl());//QZone分享参数
        oks.setVenueName("华夏云视");
        oks.setVenueDescription("这个直播很有趣,大家快来看吧!");
        oks.setLatitude(23.169f);
        oks.setLongitude(112.908f);
        // 将快捷分享的操作结果将通过OneKeyShareCallback回调
        // oks.setCallback(new OneKeyShareCallback());
        // 去自定义不同平台的字段内容
        // oks.setShareContentCustomizeCallback(new
        // ShareContentCustomizeDemo());
        // 在九宫格设置自定义的图标
        Bitmap logo = BitmapFactory.decodeResource(context.getResources(), R.mipmap.logo);
        String label = "华夏云视";
        View.OnClickListener listener = new View.OnClickListener() {
            public void onClick(View v) {

            }
        };
        oks.setCustomerLogo(logo, label, listener);

        // 为EditPage设置一个背景的View
        //oks.setEditPageBackground(getPage());
        // 隐藏九宫格中的新浪微博
        // oks.addHiddenPlatform(SinaWeibo.NAME);

        // String[] AVATARS = {
        // 		"http://99touxiang.com/public/upload/nvsheng/125/27-011820_433.jpg",
        // 		"http://img1.2345.com/duoteimg/qqTxImg/2012/04/09/13339485237265.jpg",
        // 		"http://diy.qqjay.com/u/files/2012/0523/f466c38e1c6c99ee2d6cd7746207a97a.jpg",
        // 		"http://diy.qqjay.com/u2/2013/0422/fadc08459b1ef5fc1ea6b5b8d22e44b4.jpg",
        // 		"http://img1.2345.com/duoteimg/qqTxImg/2012/04/09/13339510584349.jpg",
        // 		"http://diy.qqjay.com/u2/2013/0401/4355c29b30d295b26da6f242a65bcaad.jpg" };
        // oks.setImageArray(AVATARS);              //腾讯微博和twitter用此方法分享多张图片，其他平台不可以

        // 启动分享
        oks.show(context);
    }


}
